<template>
  <div class="login-container">
    <a-card class="login-card" title="登录">
      <a-form
        :model="formState"
        name="login"
        @finish="onFinish"
        autocomplete="off"
      >
        <a-form-item
          name="phone"
          :rules="[{ required: true, message: '请输入手机号' }]"
        >
          <a-input
            v-model:value="formState.phone"
            placeholder="请输入手机号"
            :maxlength="11"
          >
            <template #prefix>
              <mobile-outlined />
            </template>
          </a-input>
        </a-form-item>

        <a-form-item
          name="code"
          :rules="[{ required: true, message: '请输入验证码' }]"
        >
          <a-input
            v-model:value="formState.code"
            placeholder="请输入验证码"
            :maxlength="6"
          >
            <template #prefix>
              <safety-outlined />
            </template>
            <template #suffix>
              <a-button
                type="link"
                :disabled="countdown > 0"
                @click="handleSendCode"
              >
                {{ countdown > 0 ? `${countdown}秒后重试` : '获取验证码' }}
              </a-button>
            </template>
          </a-input>
        </a-form-item>

        <a-form-item>
          <a-button type="primary" html-type="submit" block>登录</a-button>
        </a-form-item>
      </a-form>
    </a-card>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { message } from 'ant-design-vue'
import { MobileOutlined, SafetyOutlined } from '@ant-design/icons-vue'
import axios from 'axios'

interface FormState {
  phone: string
  code: string
}

const formState = reactive<FormState>({
  phone: '',
  code: ''
})

const countdown = ref(0)

const startCountdown = () => {
  countdown.value = 60
  const timer = setInterval(() => {
    countdown.value--
    if (countdown.value <= 0) {
      clearInterval(timer)
    }
  }, 1000)
}

const handleSendCode = async () => {
  if (!formState.phone) {
    message.error('请输入手机号')
    return
  }
  if (!/^1[3-9]\d{9}$/.test(formState.phone)) {
    message.error('请输入正确的手机号')
    return
  }

  try {
    const response = await axios.post('/api/login/send-code', {
      phone: formState.phone
    })
    if (response.data.success) {
      message.success('验证码发送成功')
      startCountdown()
    } else {
      message.error(response.data.message || '验证码发送失败')
    }
  } catch (error) {
    message.error('验证码发送失败')
  }
}

const onFinish = async (values: FormState) => {
  try {
    const response = await axios.post('/api/login/verify', {
      phone: values.phone,
      code: values.code
    })
    if (response.data.success) {
      message.success('登录成功')
    } else {
      message.error(response.data.message || '登录失败')
    }
  } catch (error) {
    message.error('登录失败')
  }
}
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background-color: #f0f2f5;
}

.login-card {
  width: 100%;
  max-width: 400px;
}
</style> 